// 页面API服务
const path = require("path");
const fse = require("fs-extra");
const joinComponentsToProject = (str, projectCode, code) => {
  // 组件加入到项目
  return new Promise(async (resolve) => {
    const dir = path.resolve(
      __dirname,
      "../../../projects/" + projectCode + "/src/components/" + code
    );
    let sr = "";
    if (!fse.pathExistsSync(dir)) {
      await fse.outputFileSync(dir + "/index.vue", str, "utf8");
    } else {
      const stat = fse.statSync(dir);
      if (stat.isDirectory()) {
        const curl = path.resolve(dir, "index.vue");
        const txt = await fse.readFileSync(curl, "utf8");
        if (txt !== str) {
          await fse.outputFileSync(curl, str, "utf8");
        }
      } else {
        const curl = path.resolve(dir, code + ".vue");
        if (!fse.pathExistsSync(curl)) {
          fse.outputFileSync(curl, str, "utf8");
        } else {
          const txt = await fse.readFileSync(curl, "utf8");
          if (txt !== str) {
            await fse.outputFileSync(curl, str, "utf8");
          }
        }
      }
    }
    resolve(str);
  });
};

module.exports = joinComponentsToProject;
